Test Series - Data Structure

Test Number 22/115

Q: Express -15 as a 6-bit signed binary number.
A. 001111
B. 101111
C. 101110
D. 001110
Solution: The first 4 1s from the right represent the number 15, 2 more bits are padded to make it 6 digits and the leftmost bit is a 1 to represent that it is -15.
Q: Which of the following code snippet is used to convert decimal to binary numbers?
A. public void convertBinary(int num) { int bin[] = new int[50]; int index = 0; while(num > 0) { bin[index++] = num%2; num = num/2; } for(int i = index-1;i >= 0;i--) { System.out.print(bin[i]); } }
B. public void convertBinary(int num) { int bin[] = new int[50]; int index = 0; while(num > 0) { bin[++index] = num%2; num = num/2; } for(int i = index-1;i >= 0;i--) { System.out.print(bin[i]); } }
C. public void convertBinary(int num) { int bin[] = new int[50]; int index = 0; while(num > 0) { bin[index++] = num/2; num = num%2; } for(int i = index-1;i >= 0;i--) { System.out.print(bin[i]); } }
D. public void convertBinary(int num) { int bin[] = new int[50]; int index = 0; while(num > 0) { bin[++index] = num/2; num = num%2; } for(int i = index-1;i >= 0;i--) { System.out.print(bin[i]); } }
Solution: Take the modulus by 2 of the number and store in an array while halving the number during each iteration and then display the contents of the array.
Q: Which is the predefined method available in Java to convert decimal to binary numbers?
A. toBinaryInteger(int)
B. toBinaryValue(int)
C. toBinaryNumber(int)
D. toBinaryString(int)
Solution: The method toBinaryString() takes an integer argument and is defined in java.lang package. Usage is java.lang.Integer.toBinaryString(int) this returns the string representation of the unsigned integer value.
Q: Using stacks, how to obtain the binary representation of the number?
A. public void convertBinary(int num) { Stack stack = new Stack(); while (num != 0) { int digit = num / 2; stack.push(digit); num = num % 2; } System.out.print(" Binary representation is:"); while (!(stack.isEmpty() )) { System.out.print(stack.pop()); } }
B. public void convertBinary(int num) { Stack stack = new Stack(); while (num != 0) { int digit = num % 2; stack.push(digit); } System.out.print(" Binary representation is:"); while (!(stack.isEmpty() )) { System.out.print(stack.pop()); } }
C. public void convertBinary(int num) { Stack stack = new Stack(); while (num != 0) { int digit = num % 2; stack.push(digit); num = num / 2; } System.out.print(" Binary representation is:"); while (!(stack.isEmpty() )) { System.out.print(stack.pop()); } }
D. public void convertBinary(int num) { Stack stack = new Stack(); while (num != 0) { int digit = num % 2; stack.push(digit%2); num = num / 2; } System.out.print(" Binary representation is:"); while (!(stack.isEmpty() )) { System.out.print(stack.pop()); } }
Solution: Here instead of adding the digits to an array, you push it into a stack and while printing, pop it from the stack.
Q: What is the time complexity for converting decimal to binary numbers?
A. O(1)
B. O(n)
C. O(logn)
D. O(nlogn)
Solution: Since each time you are halving the number, it can be related to that of a binary search algorithm, hence the complexity is O(logn).

You Have Score    /5